package com.ooqiu.gaming.ui.main.thread.index.content;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.api.entities.Article;
import com.ooqiu.gaming.base.BaseFragment;
import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.db.Channel;
import com.ooqiu.gaming.ui.article_detail.ArticleDetailActivity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
@Route(path = IndexContentFragment.PATH)
public class IndexContentFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, IndexContentContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    public static final String PATH = "/fragment/index/content";
    @BindView(R.id.rv_index_content_list)
    RecyclerView mRvIndexContentList;
    @BindView(R.id.swiper)
    SwipeRefreshLayout mSwiper;
    IndexContentAdapter mIndexContentAdapter;
    List<Article> mArticles = new ArrayList<>();
    @Inject
    IndexContentPresenter mIndexContentPresenter;
    int page = 1;

    @Autowired(name = Constants.ROUTER_KEY_CHANNEL)
    Channel mChannel;

    @Inject
    GamingApi mGamingApi;

    @Override
    public void initInjector(View view) {
        ButterKnife.bind(this, view);
        getRouter().inject(this);
        DaggerIndexContentComponent.builder()
                .appComponent(getApplicationComponent())
                .indexContentModule(new IndexContentModule(this))
                .build()
                .inject(this);

    }

    @Override
    public int initContentView() {
        return R.layout.fragment_index_content;
    }

    @Override
    public void initUI(View view) {


    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);



        mSwiper.setOnRefreshListener(this);
        mRvIndexContentList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvIndexContentList.setAdapter(mIndexContentAdapter = new IndexContentAdapter(mArticles));

        mIndexContentAdapter.setEnableLoadMore(true);
        mIndexContentAdapter.setOnLoadMoreListener(this, mRvIndexContentList);

        mIndexContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Article article = (Article) adapter.getItem(position);
                getRouter().build(ArticleDetailActivity.PATH)
                        .withInt("news_id", article.getNews_id())
                        .navigation();
            }
        });


        onRefresh();

    }

    @Override
    public void initData() {

    }


    @Override
    protected View statusView() {
        return mSwiper;
    }

    @Override
    public void onRefresh() {
        mSwiper.setRefreshing(true);
        page = 1;
        mIndexContentPresenter.fetchArticleList(page, Integer.parseInt(mChannel.getReal_id()), true);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onRefreshComplete() {
        mSwiper.setRefreshing(false);
        mIndexContentAdapter.loadMoreComplete();
    }

    @Override
    public void haveNext() {
        mIndexContentAdapter.loadMoreEnd(false);
    }


    @Override
    public void onFetchArticleList(List<Article> articles, Boolean isRefresh) {
        if (isRefresh) {
            mArticles.clear();
        }
        page++;

        mArticles.addAll(articles);
        mIndexContentAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMoreRequested() {
        mIndexContentPresenter.fetchArticleList(page, Integer.parseInt(mChannel.getReal_id()), false);
    }


    @Override
    protected String pageExtra() {
        if (mChannel != null) {
            return "_ARTICLE_" + mChannel.getTypename();
        }
        return super.pageExtra();
    }
}
